

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Cheney">
  <meta name="keywords" content="Coding">
  
    <meta name="description" content="又称定长分区或静态分区模式，是满足多道程序设计需要的最简单的存储管理技术。基本思想：给进入主存的用户作业划分一块连续存储区域，把作业装入该连续存储区域，若有多个作业装入主存，则它们可并发执行。">
<meta property="og:type" content="article">
<meta property="og:title" content="动态分区式内存管理">
<meta property="og:url" content="https://cheney822.gitee.io/2021/05/02/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__%E5%8A%A8%E6%80%81%E5%88%86%E5%8C%BA%E5%BC%8F%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/index.html">
<meta property="og:site_name" content="Cheney blog">
<meta property="og:description" content="又称定长分区或静态分区模式，是满足多道程序设计需要的最简单的存储管理技术。基本思想：给进入主存的用户作业划分一块连续存储区域，把作业装入该连续存储区域，若有多个作业装入主存，则它们可并发执行。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/20210419114049260.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/20210419114706953.png">
<meta property="og:image" content="https://imgbed.cheney.cc/picgo/20210419114648671.png">
<meta property="og:image" content="https://gitee.com/Cheney822/images/raw/master/PicGo/2021041911465612.png">
<meta property="article:published_time" content="2021-05-02T00:22:00.000Z">
<meta property="article:modified_time" content="2022-04-05T13:14:30.559Z">
<meta property="article:author" content="Cheney">
<meta property="article:tag" content="算法">
<meta property="article:tag" content="C语言">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://imgbed.cheney.cc/picgo/20210419114049260.png">
  
  
  <title>动态分区式内存管理 - Cheney blog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"cheney822.gitee.io","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":1},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Cheney Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('https://imgbed.cheney.cc/Blog_config/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="动态分区式内存管理">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-05-02 08:22" pubdate>
        2021年5月2日 早上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      10k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      52 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">动态分区式内存管理</h1>
            
            <div class="markdown-body">
              <h2 id="背景知识："><a href="#背景知识：" class="headerlink" title="背景知识："></a>背景知识：</h2><h6 id="关于内存管理的几种方式："><a href="#关于内存管理的几种方式：" class="headerlink" title="关于内存管理的几种方式："></a>关于内存管理的几种方式：</h6><h4 id="固定分区："><a href="#固定分区：" class="headerlink" title="固定分区："></a>固定分区：</h4><p>原理：又称定长分区或静态分区模式，是满足多道程序设计需要的最简单的存储管理技术。基本思想：给进入主存的用户作业划分一块连续存储区域，把作业装入该连续存储区域，若有多个作业装入主存，则它们可并发执行。</p>
<p>使用大小相等的固定分区有两个难点：程序可能太大而不能放到一个分区中，内存的，利用率很低。由于被装入的数据块小于分区大小，从而导致分区内部有浪费现象，成为“内部碎片”。对与大小不等的分区策略，最简单的方法就是把每个进程分配到能够容纳它的最小分区中。</p>
<p>目前已经基本上没有什么场合使用固定分区。</p>
<p>优势：实现简单，只需要极少的操作系统开销</p>
<p>缺点：有内部碎片，对内存的使用不充分，活动进程的最大数目是固定的。</p>
<h4 id="可变分区："><a href="#可变分区：" class="headerlink" title="可变分区："></a>可变分区：</h4><p>可变分区存储管理不是预先把内存中的用户区域划分成若干固定分区，而是在作业要求装入内存时，根据用户作业的大小和当时内存空间使用情况决定是否为该作业分配一个分区。因此分区大小不是预先固定的，而是按作业需求量来划分的；分区的个数和位置也不是预先确定的。它有效地克服了固定分区方式中，由于分区内部剩余内存空置造成浪费的问题。<br>可变分区方式常用的内存分配算法有以下几种：</p>
<ul>
<li><p>1、 最先适应分配算法<br>　　每次分配总是顺序查找空闲区表，找到能满足长度要求的空闲区就分配。优点是实现简单，缺点是可能将大的空闲区分割成许多小的空闲区，形成许多不连续的“碎片”。碎片长度可能不能满足作业要求，降低了内存利用率。<br>　　　　　　　　改进方法，可把空闲区按地址顺序从小到大登记在空闲区表中，有利于大作业。问题是归还空区时须按地址插入表中适当位置。</p>
</li>
<li><p>2、最优适应分配算法<br>　　按作业要求从所有空闲区中挑选一个能满足要求的最小空闲区，这样保证不去分割一个更大的区域，使装入大作业时比较容易得到满足。实现办法：将空闲区按长度以递增次序登记在表中，分配时按空闲区表顺序查找即可。缺点是可能碎片更小而无法使用。回收时也要按长度扦入。</p>
</li>
<li><p>3、最坏适应分配算法<br>　　这种算法总是挑选一个最大的空闲区分割一部分给作业使用，使剩下部分不致太小，仍可供分配使用。实现办法：空闲区表中的登记项按空闲区长度递减顺序排列，按序查找分配。</p>
</li>
</ul>
<h2 id="题目描述：动态分区式存贮区管理"><a href="#题目描述：动态分区式存贮区管理" class="headerlink" title="题目描述：动态分区式存贮区管理"></a>题目描述：动态分区式存贮区管理</h2><h4 id="设计一个动态分区式存贮区管理程序，要求支持不同的放置策略。如首次、最佳、最坏。"><a href="#设计一个动态分区式存贮区管理程序，要求支持不同的放置策略。如首次、最佳、最坏。" class="headerlink" title="设计一个动态分区式存贮区管理程序，要求支持不同的放置策略。如首次、最佳、最坏。"></a>设计一个动态分区式存贮区管理程序，要求支持不同的放置策略。如首次、最佳、最坏。</h4><h4 id="说明："><a href="#说明：" class="headerlink" title="说明："></a>说明：</h4><h5 id="（1）分区描述器rd如下"><a href="#（1）分区描述器rd如下" class="headerlink" title="（1）分区描述器rd如下:"></a>（1）分区描述器rd如下:</h5><table>
<thead>
<tr>
<th>flag</th>
<th>size</th>
<th>next</th>
</tr>
</thead>
</table>
<div class="code-wrapper"><pre><code class="hljs">  要求空闲区队列按链表组织。
  主存大小假设为maxsize（单位为节=rd的大小）。 
</code></pre></div>
<h5 id="（2）主程序结构如下："><a href="#（2）主程序结构如下：" class="headerlink" title="（2）主程序结构如下："></a>（2）主程序结构如下：</h5><div class="code-wrapper"><pre><code class="hljs">  输入放置策略     
  申请一块内存作为主存
  循环处理用户的请求（包括申请、释放）
</code></pre></div>
<p>需设计两个函数处理用户请求：</p>
<ul>
<li>申请函数 Addr=Request(size)</li>
<li>释放函数 Release(addr）</li>
</ul>
<h5 id="（3）数据实例：maxsize-512"><a href="#（3）数据实例：maxsize-512" class="headerlink" title="（3）数据实例：maxsize=512"></a>（3）数据实例：maxsize=512</h5><div class="code-wrapper"><pre><code class="hljs">    J1申请162，J2申请64，J3申请120，J4申请86，J1完成，
    J3完成，J5申请72，J6申请100，J2完成，J7申请36，
    J8申请60，J4完成， J9申请110，J10申请42。
</code></pre></div>
<p>   备注：</p>
<ul>
<li>（a）所有大小其单位为节（1节=rd的大小）</li>
<li>（b）作业申请n节,实际分配的分区大小应为n+1节。 其中一节作为分区描述器，其他n节提供给作业。</li>
<li>（c）已分配区放在高地址处。</li>
<li>（d）合并时应考虑四种情况： 假设回收区为r,上邻为f1（f1需搜索自由主存队列）,下邻为f2（f2可直接计算）<div class="code-wrapper"><pre><code class="hljs">A)f1空闲，f2已分配；
B)f1已分配，f2空闲；
C)f1空闲，f2空闲；
D)f1已分配，f2已分配；
</code></pre></div>
</li>
</ul>
<h2 id="设计思路："><a href="#设计思路：" class="headerlink" title="设计思路："></a>设计思路：</h2><ul>
<li><p>向系统申请内存：<br>需要申请的内存单元的个数：totalNum<br>每个内存单元的大小：sizeof(rd)<br>程序实际向系统申请到达大小：maxsize<br>即：<br>const int totalNum = 512;//需要申请的块数<br>const int maxsize = totalNum * sizeof(rd);//实际需要申请的大小<br>rd* MainHead = (rd*)malloc(maxsize);//申请到的资源</p>
</li>
<li><p>进程向程序申请内存Request：<br>原型：rd* Request(int need,int choice);<br>参数：need表示需要申请的大小，choice表示分配时采取的方法。<br>返回值：返回一个指针，指向申请到的空间的首地址，若为空则表示申请失败。</p>
</li>
<li><p>进程释放内存Release：<br>原型：bool Release(rd* r);<br>参数：r表示待释放内存的首地址<br>返回值：返回1表示释放成功，0表示失败</p>
</li>
<li><p>可视化展示内存使用情况：<br>每一次读取用户的请求后，程序均输出当前的内存使用情况，方便用户查看，包括空闲队列的情况、已用队列的情况、内存总体分配情况。</p>
</li>
</ul>
<h2 id="数据结构："><a href="#数据结构：" class="headerlink" title="数据结构："></a>数据结构：</h2><ul>
<li>分区描述器：<br>设计一个结构体rd用来表示分区描述器，其包含：<br>Flag：分配标志，空闲为0，不为0则表示进程编号<br>Size：分区大小，分区可用字数+分区描述器大小</li>
</ul>
<p>*next：指向下一个同类型的区域</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></div></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-keyword">struct</span> <span class="hljs-title class_">rd</span> &#123;<span class="hljs-comment">//分区描述器</span><br>    <span class="hljs-type">int</span> flag = <span class="hljs-number">-1</span>;  <span class="hljs-comment">//分配标志，空闲为0，不为0则表示进程编号</span><br>    <span class="hljs-type">int</span> size = <span class="hljs-number">-1</span>;   <span class="hljs-comment">//分区大小，分区可用字数+分区描述器大小</span><br>    rd* next = <span class="hljs-literal">NULL</span>;   <span class="hljs-comment">//指向下一个同类型的区域</span><br>&#125;;<br></code></pre></td></tr></table></figure>

<ul>
<li>空闲内存队列和已用内存队列：<br>两个队列都通过链表的形式组织<br>分别为其设置头节点Free和Used：</li>
</ul>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs cpp">rd* Free = <span class="hljs-keyword">new</span> rd, * Used = <span class="hljs-keyword">new</span> rd;<span class="hljs-comment">//分别用来存空闲队列和已占用队列</span><br></code></pre></td></tr></table></figure>

<h2 id="算法设计"><a href="#算法设计" class="headerlink" title="算法设计"></a>算法设计</h2><h5 id="三种算法："><a href="#三种算法：" class="headerlink" title="三种算法："></a>三种算法：</h5><div class="code-wrapper"><pre><code class="hljs">  首次：按物理地址的顺序排列，优先地址最小的
  最佳：按照空闲容量递增排列，优先最小的
  最坏：按照空闲容量递减排列，优先最大的
</code></pre></div>
<p>三种算法在分配和释放的操作上没有区别。<br>区别在于：空闲队列变化以后需要排序，该排序对应的排序算法不同（依据是以上原理）。但在本程序中不使用排序算法，而是每次都遍历搜索满足条件的最值。</p>
<h5 id="进程向程序申请内存Request："><a href="#进程向程序申请内存Request：" class="headerlink" title="进程向程序申请内存Request："></a>进程向程序申请内存Request：</h5><p>无论是利用上述的哪种方法，都需要从已经给定的头节点开始遍历，找到&gt;=需求的一块空间。<br>找到满足上述三种算法中最优的内存块：找到内存大小比need大的内存块中地址最小的、内存最小的、内存最大的。<br>上述最优的内存块分两种情况：</p>
<ul>
<li>内存=need:这种情况只需要在free队列删除该节点，并将其加入used队列即可。</li>
<li>内存&gt;need:这种情况需要将原节点信息中的size减小need大小然后在used队列中插入一个新节点。</li>
</ul>
<h5 id="进程释放内存Release："><a href="#进程释放内存Release：" class="headerlink" title="进程释放内存Release："></a>进程释放内存Release：</h5><p>释放进程时需要分以下四种情况：假设三块连续内存分别为f1、r、f2<br>上邻为f1（f1需搜索自由主存队列）,下邻为f2（f2可直接计算）</p>
<ul>
<li>A)f1空闲，f2已分配；     //f和f1合并，修改表项f1，数量不变</li>
<li>B)f1已分配，f2空闲；     //f和f2合并，修改表项f2，数量不变</li>
<li>C)f1空闲，f2空闲；       //f1 f f2合并，修改表项f1，删除f2</li>
<li>D)f1已分配，f2已分配；   //增加空闲表项f</li>
</ul>
<p>这里设计两个信号量bool f1_is_free = 1, f2_is_free = 1;分别用来判断f1、f2是否已分配，然后程序遍历两个队列正确修改信号量的值后，程序根据这两个值分四种情况对两个队列做出相应的修改即可。</p>
<h5 id="可视化展示内存使用情况："><a href="#可视化展示内存使用情况：" class="headerlink" title="可视化展示内存使用情况："></a>可视化展示内存使用情况：</h5><p>首先遍历输出两个队列的以下信息：</p>
<ul>
<li>1.头节点相对于总内存起点的偏移量</li>
<li>2.该段内存实际大小</li>
</ul>
<p>由于本程序中插入队列元素时没有对其排序，上述输出不直观，所以继续采用以下方法输出：<br>由内存的起点开始向后同时遍历两个队列，每次找到与之最相邻的那个内存块。<br>找到内存块后由flag判断其是否已占用，由size判断这块内存的大小。<br>输出头节点相对于总内存起点的偏移量然后输出不同数量的符号表示这块空间<br>原则：未占用用‘-’表示，已占用用‘+’表示<br>输出的数量（长度）与内存大小成正比。<br>输出示例：</p>
<p><img src="https://imgbed.cheney.cc/picgo/20210419114049260.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="整体实现的代码："><a href="#整体实现的代码：" class="headerlink" title="整体实现的代码："></a>整体实现的代码：</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;iostream&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;fstream&gt;</span></span><br><span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;<br><br><span class="hljs-keyword">struct</span> <span class="hljs-title class_">rd</span> &#123;<span class="hljs-comment">//分区描述器</span><br>    <span class="hljs-type">int</span> flag = <span class="hljs-number">-1</span>;  <span class="hljs-comment">//分配标志，空闲为0，//不为0则表示进程编号</span><br>    <span class="hljs-type">int</span> size = <span class="hljs-number">-1</span>;   <span class="hljs-comment">//分区大小，分区可用字数+分区描述器大小</span><br>    rd* next = <span class="hljs-literal">NULL</span>;   <span class="hljs-comment">//对空闲区：指向下一个空闲分区，对已分配区：此项为零。</span><br>&#125;;<br>rd* Free = <span class="hljs-keyword">new</span> rd, * Used = <span class="hljs-keyword">new</span> rd;<span class="hljs-comment">//分别用来存空闲队列和已占用队列</span><br><br><span class="hljs-type">const</span> <span class="hljs-type">int</span> totalNum = <span class="hljs-number">512</span>;<span class="hljs-comment">//需要申请的块数</span><br><span class="hljs-type">const</span> <span class="hljs-type">int</span> maxsize = <span class="hljs-number">512</span> * <span class="hljs-built_in">sizeof</span>(rd);<span class="hljs-comment">//实际需要申请的大小</span><br>rd* MainHead = (rd*)<span class="hljs-built_in">malloc</span>(maxsize);<span class="hljs-comment">//申请到的资源</span><br><br><br><span class="hljs-function">rd* <span class="hljs-title">Request</span><span class="hljs-params">(<span class="hljs-type">int</span> need,<span class="hljs-type">int</span> choice)</span> </span>&#123;<span class="hljs-comment">//申请地址的函数，返回申请到的地址，为NULL则无法申请。</span><br>    <span class="hljs-comment">//size为要申请的danwei大小的个数，实际大小为size* sizeof(rd);   </span><br>    <span class="hljs-type">int</span> tem;<br>    rd* Ans = <span class="hljs-literal">NULL</span>;<br>    rd* p = <span class="hljs-literal">NULL</span>, * prep = <span class="hljs-keyword">new</span> rd, * preAns = <span class="hljs-keyword">new</span> rd;<br><br>    <span class="hljs-keyword">if</span> (choice == <span class="hljs-number">1</span>) &#123;<span class="hljs-comment">//首次算法:按物理地址的顺序排列，优先地址最小的</span><br>        tem = INT_MAX;<br>        <span class="hljs-keyword">for</span> (p = Free-&gt;next, prep-&gt;next = p; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next, prep = prep-&gt;next)<br>            <span class="hljs-keyword">if</span> ((<span class="hljs-type">int</span>)p &lt; tem &amp;&amp; p-&gt;size &gt; need)<br>                tem = (<span class="hljs-type">int</span>)p, Ans = p, preAns = prep;<br>        <span class="hljs-keyword">if</span> (Ans == <span class="hljs-literal">NULL</span>)<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;<br>    &#125;<br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (choice == <span class="hljs-number">2</span>) &#123;<span class="hljs-comment">//最佳算法按:照空闲容量递增排列，优先最小的</span><br>        tem = totalNum;<br>        <span class="hljs-keyword">for</span> (p = Free-&gt;next, prep-&gt;next = p; p != <span class="hljs-literal">NULL</span>; p=p-&gt;next, prep= prep-&gt;next)<br>            <span class="hljs-keyword">if</span> (p-&gt;size &lt; tem &amp;&amp; p-&gt;size &gt; need)<br>                tem = p-&gt;size, Ans = p, preAns = prep;<br>        <span class="hljs-keyword">if</span> (Ans == <span class="hljs-literal">NULL</span>)<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;<br>    &#125;<br>    <span class="hljs-keyword">else</span>  <span class="hljs-keyword">if</span> (choice == <span class="hljs-number">3</span>) &#123;<span class="hljs-comment">//最坏算法：按照空闲容量递减排列，优先最大的</span><br>        tem = <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">for</span> (p = Free-&gt;next, prep-&gt;next = p; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next, prep = prep-&gt;next)<br>            <span class="hljs-keyword">if</span> (p-&gt;size &gt; tem) <br>                tem = p-&gt;size, Ans = p, preAns = prep;<br>        <span class="hljs-keyword">if</span> (tem &lt; need)<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;<br>    &#125;<br><br>    <span class="hljs-comment">//修改free队列</span><br>    <span class="hljs-keyword">if</span> (Ans-&gt;size == need)<br>        preAns-&gt;next = Ans-&gt;next;<span class="hljs-comment">//删除表项</span><br>    <span class="hljs-keyword">else</span> &#123;<br>        Ans-&gt;size -= need;<br>        <span class="hljs-comment">//Ans =(Ans+Ans-&gt;size * sizeof(rd));//后一半划分出去</span><br><br> <span class="hljs-comment">//       cout&lt;&lt;&quot;当前分配地址距离开始&quot; &lt;&lt; Ans - MainHead &lt;&lt; endl;</span><br>        Ans += Ans-&gt;size;<br>    &#125;<br><br>    Ans-&gt;flag = <span class="hljs-number">1</span>;<br>    Ans-&gt;size = need;<br><br>    <span class="hljs-comment">//修改used队列</span><br>    <span class="hljs-keyword">if</span> (Used-&gt;next == <span class="hljs-literal">NULL</span>) &#123;<br>        Used-&gt;next = Ans;<br>        Ans-&gt;next = <span class="hljs-literal">NULL</span>;<br>    &#125;   <span class="hljs-keyword">else</span> &#123;<br>        Ans-&gt;next = Used-&gt;next;<br>        Used-&gt;next = Ans;<br>    &#125;<br><br>    <span class="hljs-keyword">return</span> Ans;<br>&#125;<br><span class="hljs-function"><span class="hljs-type">bool</span> <span class="hljs-title">Release</span><span class="hljs-params">(rd* r)</span> </span>&#123;<span class="hljs-comment">//释放所给出的分区的地址，返回1表示正常释放</span><br>    <span class="hljs-keyword">if</span> (r-&gt;flag == <span class="hljs-number">0</span>) &#123;<br>        cout &lt;&lt; <span class="hljs-string">&quot;该空间未使用，无法释放&quot;</span> &lt;&lt; endl;<br>        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>    &#125;<br><br>    rd* p = <span class="hljs-literal">NULL</span>;<br>    rd* f1 = <span class="hljs-literal">NULL</span>, * f2 = <span class="hljs-literal">NULL</span>;<br>   <br>    <span class="hljs-comment">//先判断used</span><br>    <span class="hljs-keyword">for</span> (p = Used-&gt;next; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next) &#123;<br>        <span class="hljs-keyword">if</span> (p-&gt;next == r)<br>            f1 = p;<br>        <span class="hljs-keyword">if</span> (r-&gt;next == p)<br>            f2 = p;<br>    &#125;<br>    <span class="hljs-comment">//再判断free</span><br>    <span class="hljs-keyword">for</span> (p = Free-&gt;next; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next) &#123;<br>        <span class="hljs-keyword">if</span> (p+p-&gt;size == r)<br>            f1 = p;<br>        <span class="hljs-keyword">if</span> (r+r-&gt;size == p)<br>            f2 = p;<br>    &#125;<br><br><br>    <span class="hljs-comment">//修改used队列</span><br>    <span class="hljs-keyword">for</span> (p = Used-&gt;next; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next)<br>        <span class="hljs-keyword">if</span> (p-&gt;next == r) &#123;<br>            p-&gt;next = r-&gt;next;<br>            <span class="hljs-keyword">break</span>;<br>        &#125;<br><br><br><br><br>    <span class="hljs-type">bool</span> f1_is_free = <span class="hljs-number">1</span>, f2_is_free = <span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">if</span> (f1-&gt;flag)<br>        f1_is_free = <span class="hljs-number">0</span>;<br>    <span class="hljs-keyword">if</span> (f2 == <span class="hljs-literal">NULL</span> || f2-&gt;flag)<br>        f2_is_free = <span class="hljs-number">0</span>;<br>    <span class="hljs-comment">//修改free队列</span><br>    <span class="hljs-keyword">if</span> (f1_is_free &amp;&amp; !f2_is_free) &#123;     <span class="hljs-comment">// A)f1空闲，f2已分配；     //f和f1合并，修改表项f1，数量不变</span><br>        f1-&gt;next = r-&gt;next;<br>        f1-&gt;size += r-&gt;size;<br>    &#125;<br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!f1_is_free &amp;&amp; f2_is_free) &#123; <span class="hljs-comment">// B)f1已分配，f2空闲；     //f和f2合并，修改表项f2，数量不变</span><br>        r-&gt;next = f2-&gt;next;<br>        r-&gt;flag = <span class="hljs-number">0</span>;<br>        r-&gt;size += f2-&gt;size;<br>        <span class="hljs-keyword">for</span> (p = Free; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next)<span class="hljs-comment">//找到f2的前驱</span><br>            <span class="hljs-keyword">if</span> (p-&gt;next == f2) &#123;<br>                p-&gt;next = r;<span class="hljs-comment">//后继修改为r</span><br>                <span class="hljs-keyword">break</span>;<br>            &#125;<br>    &#125;<br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (f1_is_free &amp;&amp; f2_is_free) &#123;  <span class="hljs-comment">// C)f1空闲，f2空闲；       //f1 f f2合并，修改表项f1，删除f2</span><br>        <span class="hljs-keyword">for</span> (p = Free; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next)<span class="hljs-comment">//找到f1的前驱</span><br>            <span class="hljs-keyword">if</span> (p-&gt;next == f1) <br>                <span class="hljs-keyword">break</span>;<br>            <br>        <span class="hljs-keyword">if</span> (p != f2-&gt;next)<span class="hljs-comment">//防止构成环路</span><br>            f1-&gt;next = f2-&gt;next;<br>        f1-&gt;size += (f2-&gt;size+r-&gt;size);<br>        <span class="hljs-keyword">for</span> (p = Free; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next)<span class="hljs-comment">//找到f2的前驱</span><br>            <span class="hljs-keyword">if</span> (p-&gt;next == f2) &#123;<br>                p-&gt;next = f2-&gt;next;<span class="hljs-comment">//后继修改为r</span><br>                <span class="hljs-keyword">break</span>;<br>            &#125;<br>    &#125;<br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!f1_is_free &amp;&amp; !f2_is_free) &#123;<span class="hljs-comment">// D)f1已分配，f2已分配；   //增加空闲表项f</span><br>        r-&gt;flag = <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">if</span> (Free-&gt;next == <span class="hljs-literal">NULL</span>) &#123;<br>            Free-&gt;next = r;<br>            r-&gt;next = <span class="hljs-literal">NULL</span>;<br>        &#125;<br>        <span class="hljs-keyword">else</span> &#123;<br>            r-&gt;next = Free-&gt;next;<br>            Free-&gt;next = r;<br>        &#125;<br>    &#125;<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;<br>&#125;<br><span class="hljs-function"><span class="hljs-type">void</span> <span class="hljs-title">show_detail</span><span class="hljs-params">()</span> </span>&#123;<span class="hljs-comment">//展示内存的使用情况</span><br>    rd* p = <span class="hljs-literal">NULL</span>;<br>    rd* Cur = MainHead;<br><br>    <span class="hljs-comment">//先判断used</span><br>    cout &lt;&lt;<span class="hljs-string">&quot;Used:--&quot;</span>;<br>    <span class="hljs-keyword">for</span> (p = Used-&gt;next; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next) &#123;<br>        cout &lt;&lt; p - MainHead &lt;&lt;<span class="hljs-string">&quot;_&quot;</span>&lt;&lt;p-&gt;size&lt;&lt; <span class="hljs-string">&quot;---&gt;&quot;</span>;<br>    &#125;<br>    cout &lt;&lt; endl;<br>    <span class="hljs-comment">//再判断free</span><br>    cout &lt;&lt; <span class="hljs-string">&quot;Free:--&quot;</span>;<br>    <span class="hljs-keyword">for</span> (p = Free-&gt;next; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next) &#123;<br>        cout &lt;&lt; p - MainHead &lt;&lt; <span class="hljs-string">&quot;_&quot;</span> &lt;&lt; p-&gt;size &lt;&lt; <span class="hljs-string">&quot;---&gt;&quot;</span>;<br>    &#125;<br>    cout &lt;&lt; endl;<br><br>    <span class="hljs-keyword">do</span>&#123;<span class="hljs-comment">//直到当前是最后一块为止</span><br><br>        <span class="hljs-keyword">for</span> (p = Free-&gt;next; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next)<br>            <span class="hljs-keyword">if</span> (Cur == p) &#123;<br>                <span class="hljs-keyword">break</span>;<br>            &#125;<br>        <span class="hljs-keyword">for</span> (p = Used-&gt;next; p != <span class="hljs-literal">NULL</span>; p = p-&gt;next)<br>            <span class="hljs-keyword">if</span> (Cur == p) &#123;<br>                <span class="hljs-keyword">break</span>;<br>            &#125;<br><br>        <span class="hljs-comment">//到此就找到了上一次的Cur后跟的块（free或者used）</span><br>        <span class="hljs-comment">//输出可视化</span><br>        <span class="hljs-comment">//512个字节，共分为26段，已用用+++++表示，未用用======表示，每段用三个符号表示</span><br>        string u = <span class="hljs-string">&quot;+++&quot;</span>;<br>        string f = <span class="hljs-string">&quot;---&quot;</span>;<br><br>        <span class="hljs-keyword">if</span> (Cur-&gt;flag == <span class="hljs-number">0</span>) &#123;<span class="hljs-comment">//为free</span><br>          <span class="hljs-comment">//  cout &lt;&lt; Cur-&gt;size;</span><br>            cout &lt;&lt; Cur - MainHead;<br>            <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; (Cur-&gt;size / <span class="hljs-number">20</span>); i++)<br>                    cout &lt;&lt; f;<br><br>        &#125;<br>        <span class="hljs-keyword">if</span> (Cur-&gt;flag != <span class="hljs-number">0</span>) &#123;<span class="hljs-comment">//为free</span><br>            cout &lt;&lt; Cur - MainHead;<br>            <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; (Cur-&gt;size / <span class="hljs-number">20</span>); i++)<br>                cout &lt;&lt; u;<br>        &#125;<br>        Cur += Cur-&gt;size;<br>    &#125; <span class="hljs-keyword">while</span> ((Cur - MainHead )  &lt; totalNum<span class="hljs-number">-1</span>);<br><br>    cout &lt;&lt; <span class="hljs-string">&quot;|||&quot;</span>;<br>    cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;<br><br><br><br> &#125;<br><br><span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>) &#123;<br>        cout &lt;&lt; <span class="hljs-string">&quot;——————————请输入放置策略————————&quot;</span> &lt;&lt; endl;<br>        cout &lt;&lt; <span class="hljs-string">&quot;—— 首次：1     最佳：2    最坏：3    退出：0——&quot;</span> &lt;&lt; endl;<br>        cout &lt;&lt; <span class="hljs-string">&quot;—————————————————————————&quot;</span> &lt;&lt; endl;<br>        <span class="hljs-type">int</span> method ;<br>        cin &gt;&gt; method;<br>        <span class="hljs-keyword">if</span>(method==<span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-built_in">system</span>(<span class="hljs-string">&quot;cls&quot;</span>);<br>            cout &lt;&lt; <span class="hljs-string">&quot;——————————Thanks!————————&quot;</span> &lt;&lt; endl;<br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (method != <span class="hljs-number">1</span> &amp;&amp; method != <span class="hljs-number">2</span> &amp;&amp; method != <span class="hljs-number">3</span>) &#123;<br>            cout &lt;&lt; <span class="hljs-string">&quot;您的输入有误，请重新输入:&quot;</span> &lt;&lt; endl;<br>            <span class="hljs-keyword">continue</span>;<br>        &#125;<br>            <br><br>        MainHead = (rd*)<span class="hljs-built_in">malloc</span>(maxsize);<span class="hljs-comment">//申请到的资源</span><br>        Free = <span class="hljs-keyword">new</span> rd, Used = <span class="hljs-keyword">new</span> rd;<span class="hljs-comment">//分别用来存空闲队列和已占用队列</span><br>       <br>        MainHead-&gt;next = <span class="hljs-literal">NULL</span>;<br>        rd* fir = MainHead;fir-&gt;next = <span class="hljs-literal">NULL</span>; fir-&gt;flag = <span class="hljs-number">0</span>;  fir-&gt;size = totalNum - <span class="hljs-number">1</span>;<br>        Free-&gt;next = fir;<br><br>        <span class="hljs-type">char</span> operat;<br>        <span class="hljs-type">int</span> id, need;<br>        fstream In;<br>        In.<span class="hljs-built_in">open</span>(<span class="hljs-string">&quot;请求.txt&quot;</span>);<br>        <span class="hljs-keyword">if</span> (!In.<span class="hljs-built_in">is_open</span>())<br>            cout &lt;&lt; <span class="hljs-string">&quot;文件打开失败&quot;</span> &lt;&lt; endl;<br>        cout &lt;&lt; <span class="hljs-string">&quot;初始状态：&quot;</span> &lt;&lt; endl; <span class="hljs-built_in">show_detail</span>();<span class="hljs-comment">//展示初始状态</span><br>        <span class="hljs-keyword">while</span> (!In.<span class="hljs-built_in">eof</span>()) &#123;<br>            In &gt;&gt; operat;<br>            <br>            <span class="hljs-keyword">if</span> (operat == <span class="hljs-string">&#x27;+&#x27;</span>) &#123;<br>                In &gt;&gt; id &gt;&gt; need;<br>                rd* req = <span class="hljs-built_in">Request</span>(need+<span class="hljs-number">1</span>, method);<span class="hljs-comment">//req为申请到的地址</span><br>                <span class="hljs-keyword">if</span> (req == <span class="hljs-literal">NULL</span>) &#123;<br>                    cout &lt;&lt; <span class="hljs-string">&quot;     失败___&quot;</span> &lt;&lt; id &lt;&lt; <span class="hljs-string">&quot; +++ &quot;</span> &lt;&lt; need &lt;&lt; <span class="hljs-string">&quot; &quot;</span> &lt;&lt; endl;<br>                    <span class="hljs-keyword">break</span>;<br>                &#125;<br>                cout &lt;&lt; <span class="hljs-string">&quot;     成功___&quot;</span> &lt;&lt;id&lt;&lt; <span class="hljs-string">&quot; +++ &quot;</span> &lt;&lt; need &lt;&lt; <span class="hljs-string">&quot; &quot;</span> &lt;&lt; endl;<br>                req-&gt;flag = id;<br>                req-&gt;size = need+<span class="hljs-number">1</span>;<br>            &#125;<br>            <span class="hljs-keyword">else</span> <br>            <span class="hljs-keyword">if</span> (operat == <span class="hljs-string">&#x27;-&#x27;</span>) &#123;<br>                In &gt;&gt; id ;<br>                rd* rel = Used-&gt;next;<br>                <span class="hljs-keyword">for</span> (; rel != <span class="hljs-literal">NULL</span>; rel=rel-&gt;next)<br>                    <span class="hljs-keyword">if</span> (rel-&gt;flag == id)<br>                        <span class="hljs-keyword">break</span>;<br>                <span class="hljs-type">int</span> rel_ans = <span class="hljs-number">1</span>;<br>                <span class="hljs-type">int</span> rel_size = rel-&gt;size;<br>                <span class="hljs-keyword">if</span> (rel == Used-&gt;next) &#123;<br>                    cout &lt;&lt; <span class="hljs-string">&quot;进程&quot;</span> &lt;&lt; id &lt;&lt; <span class="hljs-string">&quot;没有正在使用的空间&quot;</span> &lt;&lt; endl;<br>                    <span class="hljs-keyword">break</span>;<br>                &#125;<br>                <span class="hljs-keyword">else</span> <br>                    rel_ans = <span class="hljs-built_in">Release</span>(rel);<br><br>                <span class="hljs-keyword">if</span> (rel_ans == <span class="hljs-number">0</span>) &#123;<br>                    cout &lt;&lt; <span class="hljs-string">&quot;进程___&quot;</span> &lt;&lt; id &lt;&lt; <span class="hljs-string">&quot;无法释放&quot;</span> &lt;&lt; endl;<br>                &#125;<br>                <span class="hljs-keyword">else</span> <br>                    cout &lt;&lt; <span class="hljs-string">&quot;     成功___&quot;</span> &lt;&lt; id &lt;&lt; <span class="hljs-string">&quot; --- &quot;</span> &lt;&lt; rel_size &lt;&lt; <span class="hljs-string">&quot;  &quot;</span> &lt;&lt; endl;<br>                <br><br>            &#125;<br>            <span class="hljs-keyword">else</span> &#123;<br>                cout &lt;&lt; operat &lt;&lt; <span class="hljs-string">&quot;读取失败&quot;</span> &lt;&lt; endl;<br>                <span class="hljs-keyword">break</span>;<br>            &#125;<br>            <span class="hljs-built_in">show_detail</span>();<span class="hljs-comment">//展示每次修改后的结果</span><br>        &#125;<br>        <span class="hljs-built_in">free</span>(MainHead);<br>&#125;<br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><br></code></pre></td></tr></table></figure>
<h2 id="程序运行情况："><a href="#程序运行情况：" class="headerlink" title="程序运行情况："></a>程序运行情况：</h2><h5 id="测试数据："><a href="#测试数据：" class="headerlink" title="测试数据："></a>测试数据：</h5><ul>
<li><img src="https://imgbed.cheney.cc/picgo/20210419114706953.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
</ul>
<h5 id="运行截图："><a href="#运行截图：" class="headerlink" title="运行截图："></a>运行截图：</h5><ul>
<li>方法二：最佳优先算法<br><img src="https://imgbed.cheney.cc/picgo/20210419114648671.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><img src="https://gitee.com/Cheney822/images/raw/master/PicGo/2021041911465612.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
</ul>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E7%AE%97%E6%B3%95/">算法</a>
                    
                      <a class="hover-with-bg" href="/tags/C%E8%AF%AD%E8%A8%80/">C语言</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/05/03/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__%E7%A3%81%E7%9B%98%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95%EF%BC%88%E5%85%88%E6%9D%A5%E5%85%88%E6%9C%8D%E5%8A%A1%E3%80%81%E6%9C%80%E7%9F%AD%E5%AF%BB%E9%81%93%E4%BC%98%E5%85%88%E4%BB%A5%E5%8F%8A%E7%94%B5%E6%A2%AF%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95%EF%BC%89/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">磁盘调度算法（先来先服务、最短寻道优先以及电梯调度算法）</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/05/01/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F__%E9%93%B6%E8%A1%8C%E5%AE%B6%E7%AE%97%E6%B3%95/">
                        <span class="hidden-mobile">银行家算法</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://cheney822.gitee.io/" target="_blank" rel="nofollow noopener"><span>备用网址</span></a> 
  </div>
  

  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        皖ICP备2022002876号-1
      </a>
    </span>
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
